package com.qingding.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.LogInCallback;
import com.qingding.R;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private Button btnRegiste;//跳转到注册界面按钮
    private Button login;//登录按钮
    private EditText userna;//用户名输入框
    private EditText passwd;//密码输入框
    private SharedPreferences sharedPreferences;//保存数据的东西
    private String ojbectid;
    private  String userphonenumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //如果点击了注册新用户按钮,跳转到注册界面。实例化登录按钮、用户名输入框、密码输入框
        btnRegiste = (Button) findViewById(R.id.regist);
        btnRegiste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });
        login = (Button) findViewById(R.id.login);
        userna =(EditText)findViewById(R.id.username);
        passwd = (EditText)findViewById(R.id.password) ;

        //拿到存入本地的数据，获取到mobilePhoneNumber,让用户名输入框自动填写上用户名
        sharedPreferences = getSharedPreferences("_User",0);
//        ojbectid = sharedPreferences.getString("objectid","");
        userphonenumber = sharedPreferences.getString("userphonenumber","15343816435");
        userna.setText(userphonenumber);

        //为登录按钮添加事件监听器,同时判段用户输入
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                //获取用户输入的手机号和密码
                String mobilephonenumber = userna.getText().toString();
                String password = passwd.getText().toString();



                AVUser.loginByMobilePhoneNumberInBackground(mobilephonenumber, password, new LogInCallback<AVUser>() {
                    @Override
                    public void done(AVUser avUser, AVException e) {
                        if (e == null){
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            finish();
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            intent.putExtra("username",avUser.getUsername());
                            intent.putExtra("objectid",avUser.getObjectId());
                            intent.putExtra("photoid",avUser.getInt("PhotoId"));
                            startActivity(intent);

                        }else{
                            Toast.makeText(LoginActivity.this, "登录失败,请输入正确的手机号和密码", Toast.LENGTH_SHORT).show();

                        }
                    }
                });




            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
